const url = require('url');
const fs = require('fs');
const path = require('path');
const express = require('express');

const app = express();
 
app.use((req, res, next) => {
    const urlPath = url.parse(req.url).pathname || '';
    // api exists
    const apiPath = `${path.join(__dirname,  urlPath)}.json`;
  
    //   console.log(`mock api ${req.url} to ${apiPath}`)
    if (fs.existsSync(apiPath)) {
        res.setHeader('Content-Type', 'application/json');
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, token, platformid');
        res.end(fs.readFileSync(apiPath));
        return;
    }
    return next();
});
app.listen(9001,()=>{
    console.log ('mock is running： localhost:9001/');
});
